package com.mlizhi.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.StrictMode;

import com.mlizhi.base.imageloader.cache.disc.naming.Md5FileNameGenerator;
import com.mlizhi.base.imageloader.core.ImageLoader;
import com.mlizhi.base.imageloader.core.ImageLoaderConfiguration;
import com.mlizhi.base.imageloader.core.assist.QueueProcessingType;
import com.mlizhi.base.imageloader.core.download.BaseImageDownloader;
import com.mlizhi.modules.spec.dao.DaoMaster;
import com.mlizhi.modules.spec.dao.DaoMaster.DevOpenHelper;
import com.mlizhi.modules.spec.dao.DaoSession;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2015-03-13.
 */
public class MlzApplication extends Application {

	private ConcurrentHashMap<String, String> mGlobalVariables;
	private Set<AppStateListener> mAppStateListeners;
	private List<Activity> activities = new ArrayList<Activity>();
	
	private SQLiteDatabase db;
	private DaoMaster daoMaster;
    private DaoSession daoSession;

	@Override
	public void onCreate() {
		super.onCreate();
		CrashReport.initCrashReport(getApplicationContext(), "4af23c004a", false);
		// android 7.0系统解决拍照的问题
		StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
		StrictMode.setVmPolicy(builder.build());
		builder.detectFileUriExposure();
		mGlobalVariables = new ConcurrentHashMap<String, String>();
		mAppStateListeners = Collections.synchronizedSet(new HashSet<AppStateListener>());
		initImageLoader(getApplicationContext());
		DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "mlizhi-kingdom-db", null);
		db = helper.getWritableDatabase();
		daoMaster = new DaoMaster(db);
		daoSession = daoMaster.newSession();
	}
	
	public void addActivity(Activity activity) {  
        activities.add(activity);  
    }  

	public String getGlobalVariable(String key) {
		return mGlobalVariables.get(key);
	}

	public String removeGlobalVariable(String key) {
		String value = mGlobalVariables.remove(key);
		notifyListeners(key, null);
		return value;
	}

	public void putGlobalVariable(String key, String value) {
		mGlobalVariables.put(key, value);
		notifyListeners(key, value);
	}

	public void addAppStateListener(AppStateListener appStateListener) {
		mAppStateListeners.add(appStateListener);
	}

	public void removeAppStateListener(AppStateListener appStateListener) {
		mAppStateListeners.remove(appStateListener);
	}

	private void notifyListeners(String key, String value) {
		for (AppStateListener appStateListener : mAppStateListeners) {
			appStateListener.onStateChanged(key, value);
		}
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}

	public interface AppStateListener {
		void onStateChanged(String key, String value);
	}
	
	 /**初始化图片加载类配置信息**/
    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
            .threadPriority(Thread.NORM_PRIORITY - 2)//加载图片的线程数
            .denyCacheImageMultipleSizesInMemory() //解码图像的大尺寸将在内存中缓存先前解码图像的小尺寸。
            .diskCacheFileNameGenerator(new Md5FileNameGenerator())//设置磁盘缓存文件名称
            .tasksProcessingOrder(QueueProcessingType.LIFO)//设置加载显示图片队列进程
            .imageDownloader(new BaseImageDownloader(context))
//            .writeDebugLogs() // Remove for release app
            .build();
        ImageLoader.getInstance().init(config);
    }
    
    @Override  
    public void onTerminate() {  
        super.onTerminate();  
        removeActivities(); 
    }  
    
    
    public void removeActivities(){
   	 for (Activity activity : activities) {  
        	if (activity!=null) {
                activity.finish();
            } 
        }  
   }
   
	public DaoSession getDaoSession() {
		return daoSession;
	}  
   
}
